//
//  VCView.m
//  NewWaterHome
//
//  http://stackoverflow.com/questions/19185748/unable-to-setcontentoffset-in-scrollviewdidenddragging
//  http://zhangbuhuai.com/practice-in-uiscrollview/
//  Created by Leon He on 2017/5/16.
//  Copyright © 2017年 LipuWater. All rights reserved.
//

#import "VCView.h"
#import "Masonry.h"

#define SCREEN_WIDTH                [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT               [UIScreen mainScreen].bounds.size.height

#define Page_Height                 SCREEN_HEIGHT - 64

@interface VCView()<UIScrollViewDelegate>{
    int currentTab;
    int currentScrollPage;//0:up 1:down
}

@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) UIView *tabView;
@property (nonatomic, strong) UIView *arrowView;
@property (nonatomic, strong) UIView *tdsView;
@property (nonatomic, strong) UIView *recommendView;
@property (nonatomic, strong) UIScrollView *recoInnerScrollview;
@property (nonatomic, strong) UIView *recoInnerView;
@property (nonatomic, strong) UIView *filterView;

@end

@implementation VCView
@synthesize  mainScrollView, tabView, arrowView, tdsView, recommendView, filterView, recoInnerScrollview, recoInnerView;

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self initView];
    }
    return self;
}

- (void)initView{
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    titleLabel.text = @"新首页";
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLabel];
    
    mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 416)];
    mainScrollView.backgroundColor = [UIColor greenColor];
//    mainScrollView.pagingEnabled = YES;
//    mainScrollView.clipsToBounds = NO;
    mainScrollView.delegate = self;
    [self addSubview:mainScrollView];
    
    NSLog(@"Page_Height = %f", Page_Height);
    tabView = [[UIView alloc]initWithFrame:CGRectMake(0, 208 , SCREEN_WIDTH, 104)];
    tabView.backgroundColor = [UIColor yellowColor];
    [mainScrollView addSubview:tabView];
    
    for(int i = 0; i < 3; i++) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(320.0/3 * i, 0, 320.0/3, 104)];
        [button setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        button.tag = i;
        [button addTarget:self action:@selector(onTabClick:) forControlEvents:UIControlEventTouchUpInside];
        [tabView addSubview:button];
    }
    
    arrowView = [[UIView alloc]initWithFrame:CGRectMake(0, 312 , SCREEN_WIDTH, 104)];
    arrowView.backgroundColor = [UIColor purpleColor];
    [mainScrollView addSubview:arrowView];
    
    tdsView = [[UIView alloc]initWithFrame:CGRectMake(0, 416 , SCREEN_WIDTH, 312)];
    tdsView.backgroundColor = [UIColor orangeColor];
    [mainScrollView addSubview:tdsView];
    [mainScrollView setContentSize:CGSizeMake(SCREEN_WIDTH, 624)];//高度为内容的一半
    currentTab = 0;
    
    recommendView = [[UIView alloc]initWithFrame:CGRectMake(0, 416 , SCREEN_WIDTH, 312)];
    recommendView.backgroundColor = [UIColor redColor];
    //
    recoInnerScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 312)];
    recoInnerScrollview.backgroundColor = [UIColor grayColor];
    recoInnerScrollview.bounces = NO;
    [recommendView addSubview:recoInnerScrollview];
    //
    recoInnerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH, 624)];
    recoInnerView.backgroundColor = [UIColor yellowColor];
    [recoInnerScrollview addSubview:recoInnerView];
    recoInnerScrollview.contentSize = CGSizeMake(SCREEN_WIDTH, 624);
    
    filterView = [[UIView alloc]initWithFrame:CGRectMake(0, 416 , SCREEN_WIDTH, 312)];
    filterView.backgroundColor = [UIColor blueColor];
    
}

//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
//{
//    
//    UIView *view = [super hitTest:point withEvent:event];
//    if([view isKindOfClass:[UIButton class]]){
//        NSLog(@"hitTest Button");
//        return view;
//    }
//    NSLog(@"hitTest mainScrollView");
//    return mainScrollView;
//}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    float offsetY = scrollView.contentOffset.y;
    NSLog(@"scrollViewDidScroll offsetY = %f", offsetY);
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"scrollViewDidEndDragging");
    float offsetY = scrollView.contentOffset.y;
//    if(currentScrollPage == 0){
//        if(offsetY < 20){
//            [self scrollTop:offsetY];
//        } else {
//            [self scrollDown:offsetY];
//        }
//    } else {
//        if(offsetY < 188){
//            [self scrollTop:offsetY];
//        } else {
//            [self scrollDown:offsetY];
//        }
//    }
    
//    NSLog(@"scrollViewDidEndDragging");
//    CGFloat yVelocity = [scrollView.panGestureRecognizer velocityInView:scrollView].y;
//    if (yVelocity < 0) {
//        NSLog(@"Finger Up");
//    } else {
//        NSLog(@"Finger Down");
//    }
//    
//    if (yVelocity < 0 ) //UP
//    {
//        if(scrollView.contentOffset.y < 30){
//            [self scrollTop:offsetY];
//        } else {
//            [self scrollDown:offsetY];
//        }
//    }
//    else if (yVelocity > 0) //DOWN
//    {
//        if(scrollView.contentOffset.y < 178){
//            [self scrollTop:offsetY];
//        } else {
//            [self scrollDown:offsetY];
//        }
//        
//    }
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    
    float offsetY = scrollView.contentOffset.y;
    
////    targetContentOffset->x = 0;
//    if(currentScrollPage == 0){
//        if(offsetY < 20){
//            [self scrollTop:offsetY];
////            targetContentOffset->y = 0;
//            
//        } else {
//            [self scrollDown:offsetY];
////            targetContentOffset->y = 208;
//        }
//    } else {
//        if(offsetY < 188){
//            [self scrollTop:offsetY];
////            targetContentOffset->y = 0;
//        } else {
//            [self scrollDown:offsetY];
////            targetContentOffset->y = 208;
//        }
//    }
    
    NSLog(@"scrollViewDidEndDragging");
    CGFloat yVelocity = [scrollView.panGestureRecognizer velocityInView:scrollView].y;
    if (yVelocity < 0) {
        NSLog(@"Finger Up");
    } else {
        NSLog(@"Finger Down");
    }
    
    if (yVelocity < 0 ) //UP
    {
        if(scrollView.contentOffset.y < 10){
            [self scrollTop:offsetY];
        } else {
            [self scrollDown:offsetY];
        }
    }
    else if (yVelocity > 0) //DOWN
    {
        if(scrollView.contentOffset.y < 198){
            [self scrollTop:offsetY];
        } else {
            [self scrollDown:offsetY];
        }
        
    }
}

- (void)scrollTop:(float)currentOffset{
//    [UIView animateWithDuration:0.25 animations:^{
//        [mainScrollView setContentOffset:CGPointMake(0, 0) animated:NO];
//    }];
    dispatch_async(dispatch_get_main_queue(), ^{
        [mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    });
}

- (void)scrollDown:(float)currentOffset{
//    [UIView animateWithDuration:0.25 animations:^{
//        [mainScrollView setContentOffset:CGPointMake(0, 208) animated:NO];
//    }];
    dispatch_async(dispatch_get_main_queue(), ^{
        [mainScrollView setContentOffset:CGPointMake(0, 208) animated:YES];
    });
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    float offsetY = scrollView.contentOffset.y;
    NSLog(@"scrollViewDidEndDecelerating, offsetY = %f", offsetY);
    if(offsetY == 0){
        currentScrollPage = 0;
    } else if(offsetY == 208){
        currentScrollPage = 1;
    }
}

- (void)onTabClick:(UIButton*)sender{
    int index = sender.tag;
    switch (index) {
        case 0:
            if(currentTab != 0){
                [self removeScrollSubview];
                [mainScrollView addSubview:tdsView];
            }
            currentTab = 0;
            break;
        case 1:
            if(currentTab != 1){
                [self removeScrollSubview];
                [mainScrollView addSubview:recommendView];
            }
            currentTab = 1;
            break;
        case 2:
            if(currentTab != 2){
                [self removeScrollSubview];
                [mainScrollView addSubview:filterView];
            }
            currentTab = 2;
            break;
        default:
            break;
    }
}

- (void)removeScrollSubview{
    if(currentTab == 0){
        [tdsView removeFromSuperview];
    } else if(currentTab == 1){
        [recommendView removeFromSuperview];
    } else {
        [filterView removeFromSuperview];
    }
}

@end
